<template>
  <!-- 如果内容比较多可以添加top属性 -->
  <el-dialog
    title="添加用户"
    :visible.sync="dialogVisible"
    width="40%"
    :before-close="handleClose">
    <el-form ref="form" status-icon :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input style="width:70%" :disabled="disabled" @keyup.enter.native="save()" class="inputText" v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input style="width:70%" :disabled="disabled" @keyup.enter.native="save()" class="inputText" v-model="form.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="passWord">
        <el-input style="width:70%" :disabled="disabled" @keyup.enter.native="save()" type="password" class="inputText" v-model="form.passWord" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色分配" prop="roleList">
      <el-checkbox-group v-model="form.roleList">
        <el-checkbox
          :disabled="disabled"
          :key="item.id"
          :checked="item.checked"
          v-for="item in roleList"
          :label="item.id">
          {{item.name}}
        </el-checkbox>
      </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div class="bottomDiv">
      <el-button @click="handleClose">关闭</el-button>
      <el-button :disabled="disabled" type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/api'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  name: 'UserEdit',
  data () {
    return {
      roleList: [],
      form: {
        id: null,
        name: '',
        userName: '',
        passWord: '',
        roleList: []
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '请输入用户密码', trigger: 'blur' }
        ],
        roleList: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    initData () {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      this.form.id = null
      this.form.userName = null
      this.form.name = null
      this.form.passWord = null
      this.roleList = []
      this.initRolesData()
    },
    setData (data) {
      if (this.$refs.form !== undefined) {
        this.$refs.form.resetFields()
      }
      this.form.id = data.id
      this.form.name = data.name
      this.form.userName = data.userName
      this.form.passWord = data.passWord
      this.form.roleList = []
      var thiz = this
      data.roleList.forEach(role => {
        thiz.form.roleList.push(role.id)
      })
      this.initRolesData()
    },
    async updateApi () {
      var roles = []
      this.form.roleList.forEach(role => {
        var par = {
          id: role
        }
        roles.push(par)
      })
      this.form.roleList = roles
      await api.UPDATE_USER(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    async saveApi () {
      var roles = []
      this.form.roleList.forEach(role => {
        var par = {
          id: role
        }
        roles.push(par)
      })
      this.form.roleList = roles
      await api.SAVE_USER(this.form)
      this.$emit('initTableData')
      this.$message.success('操作成功')
      this.handleClose()
    },
    save () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$confirm('此操作将永久修改该数据, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.updateApi()
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消修改'
              })
            })
          } else {
            this.saveApi()
          }
        } else {
          return false
        }
      })
    },
    handleClose (done) {
      this.$emit('close')
    },
    async initRolesData () {
      var par = {}
      const res = await api.GET_ROLE_LIST(par)
      var list = res.body
      list.forEach(role => {
        role.checked = false
      })
      if (this.roleList.length > 0) {
        this.form.roleList.forEach(role => {
          list.forEach(role2 => {
            if (role === role2.id) {
              role2.checked = true
            }
          })
        })
      }
      this.roleList = list
    }
  }
}
</script>

<style>
  .inputText {
    width: 70%;
  }
  .bottomDiv {
    width: 100%;
    text-align: center;
  }
</style>
